﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using E5_KhuyenMaiVang.Models;
using System.Web.Security;

namespace E5_KhuyenMaiVang.Controllers
{
    public class MemberController : Controller
    {
        List<DEAL> DealInCart = new List<DEAL>();
        List<int> ListQuantity = new List<int>();
        GoldDealEntities db = new GoldDealEntities();
        //
        // GET: /Member/

        
        private List<MEMBER> GetMemberFromAccount()
        {
            MembershipUser user = Membership.GetUser((string)Session["LogOn"]);
            return db.MEMBER.Where(c => c.Account == (Guid)user.ProviderUserKey).ToList();
        }

        private List<VOUCHER> getVoucher() {
            List<MEMBER> members = GetMemberFromAccount();
            List<VOUCHER> vouchers = new List<VOUCHER>();
            foreach (var item in members)
            {               
                vouchers.AddRange(db.VOUCHER.Where(v => v.MemberID == item.MemberID));
            }
            return vouchers;
        }

        [Authorize(Roles = "Member")]
        public ActionResult VoucherHistory()
        {
            List<VOUCHER> vouchers = getVoucher();           
            ViewBag.StatusItems = db.VOUCHER_STATUS;
            return View(vouchers);
        }

        [HttpPost]
        public ActionResult SortedVoucherHistory(int id = 0)
        {
            ViewBag.StatusItems = db.VOUCHER_STATUS;
            List<VOUCHER> vouchers = getVoucher();
            List<VOUCHER> sortvouchers = new List<VOUCHER>();
            if (id != 0)
            {
                sortvouchers = vouchers.Where(v => v.StatusID == id).ToList();
            }
            else
            {
                sortvouchers = vouchers;
            }          
            return View("VoucherHistory", sortvouchers);
        }

        public ActionResult ViewCart()
        {
            if (Session["Cart"] != null)
            {
                List<DEAL> temp = (List<DEAL>)Session["Cart"];
                foreach (var item in temp)
                {
                    DEAL deal = db.DEAL.Single(d => d.DealID == item.DealID);
                    int Quantity = 0;
                    Quantity = (int)(item.Discount / deal.Discount);
                    ListQuantity.Add(Quantity);
                }
                Session["ListQuantity"] = ListQuantity;
                DealInCart = temp;
                ViewBag.ListQuantity = ListQuantity;
            }
            return View("Cart", DealInCart);
        }

        public ActionResult Cart(int id) {            
            if (Session["Cart"] == null)
            {
                DEAL deal = db.DEAL.Single(d => d.DealID == id);
                DealInCart.Add(deal);
                ListQuantity.Add(1);
                
            }
            else {
                List<DEAL> temp = (List<DEAL>)Session["Cart"];
                bool flag = true;
                foreach (var item in temp) {
                    DEAL deal = db.DEAL.Single(d => d.DealID == item.DealID);
                    int Quantity = 0 ;
                    if (item.DealID == id) {
                        flag = false;                        
                    }
                    Quantity = (int)(item.Discount / deal.Discount);
                    ListQuantity.Add(Quantity);
                }
                if (flag) {
                    temp.Add(db.DEAL.Single(d => d.DealID == id));
                    ListQuantity.Add(1);
                   
                }
                DealInCart = temp;
            }
            
            ViewBag.ListQuantity = ListQuantity;
            Session["Cart"] = DealInCart;
            return View(DealInCart);
        }

        public ActionResult Delete(int id)
        {
            if (Session["Cart"] != null)
            {
                List<DEAL> temp = (List<DEAL>)Session["Cart"];
                int index = -1;
                int i =-1;
                foreach (var item in temp)
                {
                    i++;
                    DEAL deal = db.DEAL.Single(d => d.DealID == item.DealID);
                    int Quantity = 0;
                    if (item.DealID == id)
                    {
                        index = i;
                    }
                    else
                    {
                        Quantity = (int)(item.Discount / deal.Discount);
                        ListQuantity.Add(Quantity);
                    }                    
                }
                temp.RemoveAt(index);
                DealInCart = temp;
                ViewBag.ListQuantity = ListQuantity;
                return View("Cart", DealInCart);
            }
            return View("Cart");
        }

        
        public ActionResult CheckOut() {
            if (Session["Cart"] != null)
            {
                List<DEAL> temp = (List<DEAL>)Session["Cart"];
                if (temp.Count != 0)
                {
                    foreach (var item in temp)
                    {
                        DEAL deal = db.DEAL.Single(d => d.DealID == item.DealID);
                        int Quantity = 0;
                        Quantity = (int)(item.Discount / deal.Discount);
                        ListQuantity.Add(Quantity);
                    }
                    Session["ListQuantity"] = ListQuantity;
                    return RedirectToAction("StepOne", "Checkout");
                }
            }
            return RedirectToAction("Index", "Home");
        }

        [HttpPost]
        public ActionResult DeleteAll() {
            Session["Cart"] = null;
            return RedirectToAction("Index", "Home");
        }

        [HttpPost]
        public ActionResult EditCart(int id, int count)
        {
            if (Session["Cart"] != null)
            {                
                List<DEAL> temp = (List<DEAL>)Session["Cart"];
                
                foreach (var item in temp) {
                    DEAL deal = db.DEAL.Single(d => d.DealID == item.DealID);
                    int Quantity = 0 ;
                    if (item.DealID == id)
                    {                       
                        item.Discount = deal.Discount * count;
                        Quantity = count;
                    }
                    else {
                        Quantity = (int)(item.Discount / deal.Discount);
                    }
                    ListQuantity.Add(Quantity);
                }
                DealInCart = temp;
                ViewBag.ListQuantity = ListQuantity;
                return View("Cart",DealInCart);
            }
            return View("Cart");
        }

        [Authorize(Roles = "Member")]
        public ActionResult Edit() {
            MembershipUser user = Membership.GetUser((string)Session["LogOn"]);
            MEMBER m = db.MEMBER.First(a => a.Account == (Guid)user.ProviderUserKey);
            MemberView mv = MemberManager.ToMemberView(m);            
            return View(mv);
        }

        [Authorize(Roles = "Member")]
        public ActionResult EditComplete(MemberView mv)
        {
            MembershipUser user = Membership.GetUser((string)Session["LogOn"]);
            MEMBER m = db.MEMBER.First(a => a.Account == (Guid)user.ProviderUserKey);
            MemberManager.ToMember(m, mv);
            db.SaveChanges();
            return PartialView("EditSuccess");
        }        

        [HttpPost]
        public ActionResult GetMore() {
            return RedirectToAction("Index", "Home");
        }
    }
}
